package cn.hejiang.quartz.service.impl;

import cn.hejiang.quartz.jobs.MainJob;
import cn.hejiang.quartz.service.IQuartzService;
import cn.hejiang.quartz.utils.QuartzJobInfo;
import cn.hejiang.quartz.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzServiceImpl implements IQuartzService {

    @Autowired
    private SchedulerFactoryBean factoryBean;

    @Override
    public void addJob(QuartzJobInfo jobInfo) {
        Scheduler scheduler = factoryBean.getScheduler();
        String jobName = jobInfo.getJobName();
        Class cls = MainJob.class;
        Object params = jobInfo.getParams();  // 参数
        String time = jobInfo.getCronj();
        QuartzUtils.addJob(scheduler, jobName, cls, params, time);

    }

    @Override
    public void removeJob(String jobName) {
        System.out.println("主动取消还未执行的定时任务" + jobName);
        QuartzUtils.removeJob(factoryBean.getScheduler(), jobName);
    }
}
